﻿using Admin.IRepository;
using Admin.IService;
using Admin.Model.Tool;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Admin.Service
{
    public class BaseServices<TEntity> : IBaseServices<TEntity> where TEntity : class, new()
    {
        public IBaseRepository<TEntity> _repository;
        public BaseServices(IBaseRepository<TEntity> repository)
        {
            _repository = repository;
        }
        #region 增删改
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model">参数实体</param>
        /// <returns>主键ID</returns>
        public async Task<int> Insert(TEntity model)
        {
            if (model != null)
            {
                return await _repository.Insert(model);
            }
            return 0;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model">参数实体</param>
        /// <param name="fields">新增的字段</param>
        /// <returns>主键ID</returns>
        public async Task<int> Insert(object param, string[] fields)
        {
            if (param != null && fields.Length > 0)
            {
                return await _repository.Insert(param, fields);
            }
            return 0;
        }
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<int> Insert(List<TEntity> list)
        {
            if (list != null && list.Count > 0)
            {
                return await _repository.Insert(list);
            }
            return 0;
        }
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="list"></param>
        /// <param name="fields">新增的字段</param>
        /// <returns></returns>
        public async Task<int> Insert(List<TEntity> list, string[] fields)
        {
            if (list != null && list.Count > 0)
            {
                return await _repository.Insert(list, fields);
            }
            return 0;
        }
        /// <summary>
        /// 根据主键修改
        /// </summary>
        /// <param name="model">参数实体</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Edit(TEntity model)
        {
            if (model != null)
            {
                //return await _repository.Edit(model);
            }
            return 0;
        }

        /// <summary>
        /// 根据主键修改部分字段 
        /// </summary>
        /// <param name="model">参数实体</param>
        /// <param name="fields">修改的字段</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Edit(object param, string[] fields)
        {
            if (param != null && fields.Length > 0)
            {
                return await _repository.Edit(param, fields);
            }
            return 0;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model">实体</param>
        /// <param name="fields">修改的字段</param>
        /// <param name="sqlWhere">修改的条件:参数化写法，值放param里传</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Edit(object param, string[] fields, string sqlWhere)
        {
            if (param != null && fields.Length > 0 && !string.IsNullOrWhiteSpace(sqlWhere))
            {
                return await _repository.Edit(param, fields, sqlWhere);
            }
            return 0;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Code">主键ID</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Delete(int Code)
        {
            if (Code > 0)
            {
                return await _repository.Delete(Code);
            }
            return 0;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="param">参数实体</param>
        /// <param name="sqlWhere">where条件，参数化写法，值放param里传</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Delete(object param, string sqlWhere)
        {
            if (param != null && !string.IsNullOrWhiteSpace(sqlWhere))
            {
                return await _repository.Delete(param, sqlWhere);
            }
            return 0;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">主键ids 多个逗号隔开</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> Delete(string ids)
        {
            if (!string.IsNullOrWhiteSpace(ids))
            {
                return await _repository.Delete(ids);
            }
            return 0;
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据主键查询单条
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Code">主键ID</param>
        /// <param name="cacheEntity"></param>
        /// <returns>单条数据</returns>
        public async Task<TEntity> Get(int Code, CacheModel cacheEntity = null)
        {
            if (Code > 0)
            {
                return await _repository.Get(Code, cacheEntity);
            }
            return default(TEntity);
        }

        /// <summary>
        /// 单条查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param">参数实体</param>
        /// <param name="sqlWhere">where条件，参数化写法，值放param里传</param>
        /// <param name="showFields">显示字段</param>
        /// <param name="cacheEntity">是否缓存实体</param>
        /// <returns>单条数据</returns>
        public async Task<TEntity> Get(object param, string sqlWhere, string showFields = "*", CacheModel cacheEntity = null)
        {
            return await _repository.Get(param, sqlWhere, showFields, cacheEntity);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheEntity">缓存实体</param>
        /// <returns>数据列表</returns>
        public async Task<IEnumerable<TEntity>> GetList(CacheModel cacheEntity = null)
        {
            return await _repository.GetList(cacheEntity);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="param">参数实体</param>
        /// <param name="sqlWhere">where条件，参数化写法，值放param里传</param>
        /// <param name="orderBy">排序：Id desc,Code asc</param>
        /// <param name="showFields">显示字段</param>
        /// <param name="cacheEntity">是否缓存实体</param>
        /// <returns>数据列表</returns>
        public async Task<IEnumerable<TEntity>> GetList(object param, string sqlWhere, string orderBy, string showFields = "*", CacheModel cacheEntity = null)
        {
            return await _repository.GetList(param, sqlWhere, orderBy, showFields, cacheEntity);
        }

        /// <summary>
        /// 获取前几条数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="TopNumber">数量</param>
        /// <param name="param">参数实体</param>
        /// <param name="sqlWhere">where条件，参数化写法，值放param里传</param>
        /// <param name="orderBy">排序：Id desc,Code asc</param>
        /// <param name="showFields">显示字段</param>
        /// <param name="cacheEntity">是否缓存实体</param>
        /// <returns>数据列表</returns>
        public async Task<IEnumerable<TEntity>> GetTopList(int TopNumber, object param, string sqlWhere, string orderBy, string showFields = "*", CacheModel cacheEntity = null)
        {
            if (TopNumber > 0)
            {
                return await _repository.GetTopList(TopNumber, param, sqlWhere, orderBy, showFields, cacheEntity);
            }
            return null;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">当前页</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="param">条件参数</param>
        /// <param name="sqlWhere">条件</param>
        /// <param name="orderBy">排序</param>
        /// <param name="showFields">显示字段</param>
        /// <param name="cacheEntity">缓存</param>
        /// <returns></returns>
        public async Task<IEnumerable<TEntity>> GetPage(int page, int pageSize, object param = null, string sqlWhere = "", string orderBy = "", string showFields = "*", CacheModel cacheEntity = null)
        {
            return await _repository.GetPage(page, pageSize, param, sqlWhere, orderBy, showFields, cacheEntity);
        }
        #endregion

        #region  其他
        /// <summary>
        /// 检测是否存在
        /// </summary>
        /// <param name="param">参数实体</param>
        /// <param name="sqlWhere">where条件，参数化写法，值放param里传</param>
        /// <returns>true or false</returns>
        public async Task<bool> IsExist(object param, string sqlWhere)
        {
            return await _repository.IsExist(param, sqlWhere);
        }

        /// <summary>
        /// 查询总数量
        /// </summary>
        /// <param name="param">参数实体</param>
        /// <param name="sqlWhere">where条件，参数化写法，值放param里传</param>
        /// <returns>数量</returns>
        public async Task<int> GetCount(object param, string sqlWhere)
        {
            return await _repository.GetCount(param, sqlWhere);
        }
        #endregion
    }
}
